<?php
/*
	分佣中心
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$this->session('footernav','');
if($_RQ['op']=='main'){
	$return['ischecklogin']=model('jy_weishop/member')->checklogin('','','','',true);
	$this->model('yufee')->up_tixian($_SESSION['uid']);
	$return['registerurl']=$return['pluginset']['app_saleurl']?:'';
	$_SESSION['forward_url']='jy_newcar/gou.index/';
	$_SESSION['logout_forward']='jy_newcar/gou.index/';
	$this->model('member')->update_agent_level($_SESSION['uid']);
	$return['setting']=$this->plugin_setting('page_setting');
	$diytitle=array('tagent','tshop','tmyshop','tcenter','tbecome','balance_button','tcommission','tcommission1','commission_total','commission_1','balance_total','balance_1','commission_1_1','commission_0','balance_2','balance_3','balance_pay','tcommission_detail','torder','tdown','tmydown','area_title','commission_area');
	foreach($diytitle as $v){
		$return['setting'][$v]=$return['setting'][$v.'2']?:$return['setting'][$v];
	}
	$return['xieyi']=$this->plugin_setting('xieyi');
	$return['tagent']=$tagent;
	set_html('title','购车合伙人');
	$basic=model('core/member')->basic($_SESSION['uid']);
	$basic['username']=$_SESSION['member']['username'];
	$member=array_merge($member,$basic);
	if($member['agent_id']){
		$member['agent']=model('core/member')->basic($member['agent_id']);
	}else{
		$member['agent']=array('nickname'=>'总店');
	}
	$return['ordernum']=0;
	if($member['t_time']  >0){
    	$member['b_time'] = date('Y-m-d',$member['t_time']);
	}else{
	    $member['b_time'] = 0;
	}
	if($return['pluginset']['app_saleurl']){
		$return['pluginset']['app_saleurl2']=str_replace('appregister','getapp',$return['pluginset']['app_saleurl']);
		$return['pluginset']['app_saleurl2'].='&snmid='.$member['snmid'];
	}
	$return['pluginset']['app_saleurl']=$return['pluginset']['app_saleurl']?:'jy_newcar/index//fromuser='.$_SESSION['uid'];
	$return['pluginset']['app_saleurl'].='&snmid='.$member['snmid'];
	$member['fromurl']=custom_url($return['pluginset']['app_saleurl'],true);
	$return['member']=$member;
	$data=array();
	$data['commission_total']=$this->sum('commission',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'type >'=>1),'money');
	$data['qinum']=$this->count('yufee',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'status'=>0,'type'=>0));
	$data['yuefee1']=$this->sum('yufee',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>0),'money');
	$data['yuefee2']=$this->sum('yufee',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'status'=>2,'type'=>0),'money');
	$data['tuifee0']=$this->sum('commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'type'=>0),'money');
	$data['balance_num']=pdo_count('core_member_cash',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$data['credit2inall']=pdo_getsum('core_credit_log',array('type'=>2,'is_add'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'fee');
	$data['credit2inday']=pdo_getsum('core_credit_log',array('type'=>2,'is_add'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'createtime >'=>$daytime),'fee');
	$data['credit2outall']=pdo_getsum('core_credit_log',array('type'=>2,'is_add'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'fee');
	$data['credit2outday']=pdo_getsum('core_credit_log',array('type'=>2,'is_add'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'createtime >'=>$daytime),'fee');
	$data['credit3inall']=pdo_getsum('core_credit_log',array('type'=>3,'is_add'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'fee');
	$data['credit3inday']=pdo_getsum('core_credit_log',array('type'=>3,'is_add'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'createtime >'=>$daytime),'fee');
	$data['credit3outall']=pdo_getsum('core_credit_log',array('type'=>3,'is_add'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'fee');
	$data['credit3outday']=pdo_getsum('core_credit_log',array('type'=>3,'is_add'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'createtime >'=>$daytime),'fee');
	$levels=array();
	$teammids=$this->model('member')->getteamids($_SESSION['uid']);
	$return['member']['team_num']=count($teammids);
	$return['member']['member_num']=pdo_count('jy_newcar_member',array('agent_id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	foreach($data as &$row){
		$row=price($row);
	}
	$data['qinum']=intval($data['qinum']);
	$return['data']=$data;
	//公告列表
	$pindex = max(1, intval($post['page']));
	$psize = 10;
	$total='';
	$notice_list = pdo_getslice('jy_weishop_notice',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array($pindex,20),$total,array('id','title','createtime'),'','id desc');
	if($notice_list){
		foreach($notice_list as &$notice){
			$notice['createtime']=date('Y-m-d h:i',$notice['createtime']);
		}
	}
	$return['notice_list']=$notice_list;
	$status0 = pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>0,'plugin_name'=>'jy_newcar'));
	$status1 = pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1,'plugin_name'=>'jy_newcar'));
	$status2 = pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>2,'plugin_name'=>'jy_newcar'));
	$status3 = pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>3,'plugin_name'=>'jy_newcar'));
	$cartnums= pdo_getsum('jy_weishop_member_cart',array('mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
	$return['statics']=array(
			'status0'=>$status0,
			'status1'=>$status1,
			'status2'=>$status2,
			'status3'=>$status3,
			'cartnums'=>$cartnums
	);
	$return['rank']=$this->plugin_setting('rank');
	$return['dayorderfee']=$this->model('model')->get_dayfeelog();
	$return['mymembers']=pdo_getall('core_members',array('id <>'=>$_SESSION['uid'],'mobile'=>$member['mobile'],'uniacid'=>$_SESSION['uniacid']),array('id','username'));
	$htmls=array(
		array('type'=>'data','num'=>12,'one_num'=>3,'title'=>'总额/数量','datas'=>array(
			array('title'=>'总分佣','num'=>2,'data'=>array($data['fee_3'],$data['fee_3num'])),
			array('title'=>'已结算','yuan'=>1,'num'=>2,'data'=>array($data['balance_total'],$data['balance_num'])),
			array('title'=>'待结算','yuan'=>1,'num'=>2,'data'=>array($data['commission_1'],$data['commission_1num'])),
			array('title'=>'销售人数','num'=>2,'data'=>array($allmember_num,$allmember_num)),
		)),
	);
	$order = pdo_get('jy_weishop_order',array('api_data like'=>'%"buytype":"0"%','uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>array(1,2,3)),'','id desc,status asc');
	$order['api_data']=json_decode($order['api_data'],true);
	if($order['id']){
		$order['statustitle']=$this->order_status[$order['status']];
		$order['send_statustitle']=$this->send_status[$order['send_status']];
		$order['buytypetitle']=$this->buytype[$order['api_data']['buytype']];
		$order['payday']=date('Y-m-d',$order['paytime']);
	}
	$return['noworder']=$order;
	$return['shopset']=plugin('jy_weishop')->plugin_setting('shopset');
	$return['orderlist_url']='jy_newcar/order.list/';
	exi($return);
}

if($_RQ['op']=='zhuan'){
	$post=get_postdata();
	$type=3;
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']));
	if(!$post['fee']){
		exi('请输入转账额度！','error');
	}
	if(!$post['mid']){
		exi('请选择收款人！','error');
	}
	if($member['credit3']<$post['fee']){
		exi('转账额度不能大于，当前剩余额度！','error');
	}
	$tomember=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['mid']));
	$tomid=$tomember['id'];
	if(!$tomid){
		exi('没找到收款人，转账失败！','error');
	}
	model('core/member')->member_credit($_SESSION['uid'],'credit3',$post['fee'],false,'core','转账给:'.$tomember['username']);
	model('core/member')->member_credit($tomid,'credit3',$post['fee'],true,'core',$member['username'].'的转账');
	exi('转账成功！');
}
?>